package com.battcn.platform.service.sys.impl;

import com.battcn.framework.mybatis.mapper.BaseMapper;
import com.battcn.framework.mybatis.page.DataGrid;

import com.battcn.platform.pojo.RecordEntity;
import com.battcn.platform.pojo.dto.sys.ManagerDto;
import com.battcn.platform.service.sys.BaseService;
import com.battcn.platform.util.GenIdUtil;
import com.battcn.platform.util.SessionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 基础操作
 *
 * @param <T> 泛型
 * @author Levin
 * @version 2.5.1
 * @since 2018-01-10
 */
@Service
public abstract class BaseServiceImpl<T extends RecordEntity> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    //添加数据前执行
    public void insertBefore(T entity){
        entity.setId(GenIdUtil.uuid());
        entity.setCreateDate(new Date());   //创建时间
        ManagerDto user = SessionUtil.getSession();
        entity.setCreateBy(user.getId());   //创建人
        entity.setUpdateBy(user.getId());   //修改人
        entity.setUpdateDate(new Date());   //修改时间
    }
    //修改数据前执行
    public void updateBefore(T entity){
        ManagerDto user = SessionUtil.getSession();
        entity.setUpdateBy(user.getId());   //修改人
        entity.setUpdateDate(new Date());   //修改时间
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insert(T entity) {
        this.insertBefore(entity);
        return mapper.insertSelective(entity) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertSelective(T entity) {
        this.insertBefore(entity);
        return mapper.insertSelective(entity) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Object key) {
        return mapper.deleteByPrimaryKey(key) > 0;
    }

    @Override
    public Optional<T> selectById(Object key) {
        return Optional.ofNullable(mapper.selectByPrimaryKey(key));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateById(T entity) {
        this.updateBefore(entity);
        return mapper.updateByPrimaryKey(entity) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateSelectiveById(T entity) {
        this.updateBefore(entity);
        return mapper.updateByPrimaryKeySelective(entity) > 0;
    }

    @Override
    public List<T> listAll() {
        return this.mapper.selectAll();
    }

    @Override
    public PageInfo<T> listForDataGrid(DataGrid grid) {
        return this.listForDataGrid(grid, null);
    }

    @Override
    public PageInfo<T> listForDataGrid(DataGrid grid, T entity) {
        grid.getOrderBy();
        PageHelper.startPage(grid.getPageNum(), grid.getPageSize());
        return new PageInfo<>(mapper.select(entity));
    }


}
